﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using ModelCheckUtility.ModelLayer;
using ModelCheckUtility.Properties;

namespace ModelCheckUtility.ControlLayer
{
	public static class ElementPackerManger
	{
		public static Dictionary<int, BaseElementPacker> DicNowValues
		{
			get
			{
				return ElementPackerManger.m_dicNowValues;
			}
		}

		public static Dictionary<int, Dictionary<int, BaseElementPacker>> NowValueStructure
		{
			get
			{
				return ElementPackerManger.m_dicdicnowValueStructure;
			}
		}

		public static List<int> LstAddRoomIds
		{
			get
			{
				return ElementPackerManger.m_lstAddRoomIds;
			}
			set
			{
				ElementPackerManger.m_lstAddRoomIds = value;
			}
		}

		public static void ReSet()
		{
			ElementPackerManger.m_nNowCanUseIndex = 0;
			ElementPackerManger.m_stacknDeleteKeys = new Stack<int>();
			ElementPackerManger.m_dicNowValues = new Dictionary<int, BaseElementPacker>();
			ElementPackerManger.m_dicdicnowValueStructure = new Dictionary<int, Dictionary<int, BaseElementPacker>>();
		}

		public static List<int> GetAllAddedRoomId()
		{
			List<int> list = new List<int>();
			for (int i = 0; i < ElementPackerManger.DicNowValues.Keys.Count; i++)
			{
				int key = ElementPackerManger.DicNowValues.Keys.ElementAt(i);
				BaseRoomPacker baseRoomPacker = ElementPackerManger.DicNowValues[key] as BaseRoomPacker;
				if (baseRoomPacker != null && baseRoomPacker.IfAdded)
				{
					list.Add(baseRoomPacker.ElementId);
				}
			}
			return list;
		}

		public static void TryDeletedAddedRoom(Document useDoc, List<int> lstDontDeleteRoomId)
		{
			for (int i = 0; i < ElementPackerManger.DicNowValues.Keys.Count; i++)
			{
				int key = ElementPackerManger.DicNowValues.Keys.ElementAt(i);
				BaseRoomPacker baseRoomPacker;
				try
				{
					baseRoomPacker = (ElementPackerManger.DicNowValues[key] as BaseRoomPacker);
				}
				catch (Exception)
				{
					baseRoomPacker = null;
				}
				if (baseRoomPacker != null && baseRoomPacker.IfAdded && useDoc.GetElement(baseRoomPacker.ThisRoom.Id) != null && !lstDontDeleteRoomId.Contains(baseRoomPacker.ThisRoom.Id.IntegerValue))
				{
					useDoc.Delete(baseRoomPacker.ThisRoom.Id);
				}
			}
		}

		internal static int AddElementPacker(BaseElementPacker inputValue)
		{
			int nNowCanUseIndex = ElementPackerManger.m_nNowCanUseIndex;
			int linkInstanceId = inputValue.LinkInstanceId;
			int elementId = inputValue.ElementId;
			if ((-1 != linkInstanceId || -1 != elementId) && !(inputValue is BaseElementPacker_NoneInDic))
			{
				if (ElementPackerManger.NowValueStructure.ContainsKey(linkInstanceId))
				{
					if (ElementPackerManger.NowValueStructure[linkInstanceId].ContainsKey(elementId))
					{
						return ElementPackerManger.NowValueStructure[linkInstanceId][elementId].ThisElementPackerId;
					}
					ElementPackerManger.NowValueStructure[linkInstanceId].Add(elementId, inputValue);
				}
				else
				{
					ElementPackerManger.NowValueStructure.Add(linkInstanceId, new Dictionary<int, BaseElementPacker>());
					ElementPackerManger.NowValueStructure[linkInstanceId].Add(elementId, inputValue);
				}
			}
			ElementPackerManger.DicNowValues.Add(ElementPackerManger.m_nNowCanUseIndex, inputValue);
			ElementPackerManger.m_nNowCanUseIndex = ElementPackerManger.FindANowCanUseIndex();
			return nNowCanUseIndex;
		}

		internal static BaseElementPacker FindSameBaseElementPacker(Element inputElement, RevitLinkInstance inputLinkInstance = null)
		{
			int elementNumber = int.Parse(Resources.Str_ValueOfNoneElementId);
			int linkInstanceNumber = int.Parse(Resources.Str_ValueOfNoneElementId);
			if (inputElement != null)
			{
				elementNumber = inputElement.Id.IntegerValue;
				if (inputLinkInstance != null)
				{
					linkInstanceNumber = inputLinkInstance.Id.IntegerValue;
				}
			}
			return ElementPackerManger.FindSameBaseElementPacker(elementNumber, linkInstanceNumber);
		}

		internal static BaseElementPacker GetBaseElementPacker(int id)
		{
			if (!ElementPackerManger.DicNowValues.Keys.Contains(id))
			{
				return null;
			}
			return ElementPackerManger.DicNowValues[id];
		}

		internal static bool TryDeleteElementPacker(int keyValue)
		{
			if (!ElementPackerManger.DicNowValues.Keys.Contains(keyValue))
			{
				return false;
			}
			BaseElementPacker baseElementPacker = ElementPackerManger.DicNowValues[keyValue];
			ElementPackerManger.DicNowValues.Remove(keyValue);
			ElementPackerManger.m_stacknDeleteKeys.Push(keyValue);
			if (!(baseElementPacker is BaseElementPacker_NoneInDic))
			{
				if (!ElementPackerManger.NowValueStructure.ContainsKey(baseElementPacker.LinkInstanceId) || !ElementPackerManger.NowValueStructure[baseElementPacker.LinkInstanceId].ContainsKey(baseElementPacker.ElementId))
				{
					return false;
				}
				ElementPackerManger.NowValueStructure[baseElementPacker.LinkInstanceId].Remove(baseElementPacker.ElementId);
			}
			return true;
		}

		private static BaseElementPacker FindSameBaseElementPacker(int elementNumber, int linkInstanceNumber)
		{
			BaseElementPacker result = null;
			if (ElementPackerManger.NowValueStructure.ContainsKey(linkInstanceNumber) && ElementPackerManger.NowValueStructure[linkInstanceNumber].ContainsKey(elementNumber))
			{
				result = ElementPackerManger.NowValueStructure[linkInstanceNumber][elementNumber];
			}
			return result;
		}

		private static int FindANowCanUseIndex()
		{
			int num = 0;
			if (0 < ElementPackerManger.DicNowValues.Count)
			{
				num = ElementPackerManger.DicNowValues.Keys.Max();
				num++;
			}
			int result;
			if (0 < ElementPackerManger.m_stacknDeleteKeys.Count)
			{
				result = ElementPackerManger.m_stacknDeleteKeys.Pop();
			}
			else
			{
				result = num;
			}
			return result;
		}

		private static int m_nNowCanUseIndex = 0;

		private static Stack<int> m_stacknDeleteKeys = new Stack<int>();

		private static List<int> m_lstAddRoomIds = new List<int>();

		private static Dictionary<int, BaseElementPacker> m_dicNowValues = new Dictionary<int, BaseElementPacker>();

		private static Dictionary<int, Dictionary<int, BaseElementPacker>> m_dicdicnowValueStructure = new Dictionary<int, Dictionary<int, BaseElementPacker>>();
	}
}
